package cn.itcast.web.controller;

import cn.itcast.domain.company.Company;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Remark;
import cn.itcast.domain.system.User;
import cn.itcast.service.company.RemarkService;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.rmi.activation.UnknownObjectException;
import java.util.List;

/**
 * 登录控制器
 */
@Controller
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private ModuleService moduleService;

    @Reference
    private RemarkService remarkService;



    /**
     * 登录方法
     * 跳转到后台首页 home/main
     */
    @RequestMapping("/login")
    public String login(String email,String password,String check){
        //判断邮箱与密码是否为空，如果为空跳回登录页面
        if (StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
            request.setAttribute("error","用户名或者密码不能为空");
            return "forward:/login.jsp";
        }
        //1.判断验证码是否正确
        //获取用户提交的用户名

        //获取会话域中验证码
        HttpSession session = request.getSession();
        String vcode = (String) session.getAttribute("vcode");
        if (check==null){
            request.setAttribute("error","验证码不能为空！");
            return "forward:/login.jsp";
        }
        if (!check.equalsIgnoreCase(vcode)) {
//                out.print("验证码错误");
            request.setAttribute("error","验证码错误！");
            return "forward:/login.jsp";
        }

        try {

            //获取subject对象
            Subject subject = SecurityUtils.getSubject();

            //把用户名与密码封装到一个token对象中
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(email, password);
            //使用subject对象把token发出登陆认证请求，其实本质上就是token传递给了realm
            subject.login(usernamePasswordToken);
            //realm认证完毕之后会给你返回一个登陆成功用户对象，shiro在你登陆成功后自己会在session中设置很多登陆成功标记
            User loginUser = (User) subject.getPrincipal();
            //shiro本质上已经做了登陆成功标记，但是由于我们自己的代码也需要使用登录者的信息，所以我们自己也会在session中做登录者信息存储
            session.setAttribute("loginUser",loginUser);
            List<Module> moduleList = moduleService.findModuleByUser(loginUser);
            session.setAttribute("modules",moduleList);
            return "home/main";
        }catch (UnknownAccountException e){
            //一旦出现了unkownAccountException带用户名不存在
            request.setAttribute("error","用户名不存在");
            return "forward:/login.jsp";
        }catch (IncorrectCredentialsException e){
            //一旦出现了IncorrectCredentialsException 代表密码错误
            request.setAttribute("error","密码错误");
            return "forward:/login.jsp";
        }

    }
    //该方法任务：让后台首页的内容区域去到home/home,jsp
    @RequestMapping("/home")
    public String home(){
        return "home/home";//完整路径：/WEB-INF/pages/home/home.jsp
    }

    /**
     * url：/logout.do
     * 参数：没有
     * 作用：退出登录
     * @return 登录页面
     */
    @RequestMapping("/logout")
    public String logout(){
        //先使用shiro的方法销毁shiro登陆成功标记
        Subject subject = SecurityUtils.getSubject();
        subject.logout(); //销毁shiro在session的标记，然后你才能摧毁session
        //退出登录的本质就是销毁session的登录成功标记
        //直接把整个session销毁
        session.invalidate();
        return "redirect:/login.jsp";
    }

    /**
     * 关于系统简介
     */
    @RequestMapping("/system")
    public String system(){
        return "redirect:/system.jsp";
    }
    /**
     * 留言板
     */
    @RequestMapping("/leave")
    public String leave(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        List<Remark> remarkList = remarkService.findAll();

        request.setAttribute("userList",remarkList);
        return "forward:/leave.jsp";
    }
    /**
     * 留言板留言
     */
    @RequestMapping("/addLeave")
    public String addLeave(Remark remark){
        remarkService.save(remark);

        return "redirect:/leave.do";
    }


}
